class RelativeMemoToOpenSourceProject < ActiveRecord::Base
  # attr_accessible :title, :body
  belongs_to :open_source_project, :foreign_key => "osp_id"
  belongs_to :topic, :class_name => 'RelativeMemo', :foreign_key => 'relative_memo_id'
  has_one :relative_memos, :class_name => 'RelativeMemo', :foreign_key => 'id'
  has_many :no_uses, :as => :no_use, :dependent => :delete_all
  set_primary_key "id"
  validates_presence_of :osp_id, :relative_memo_id

  scope :no_use_for, lambda { |user_id|
    {:include => :no_uses,
     :conditions => ["#{NoUse.table_name}.user_id = ?", user_id] }
  }
  
  def self.getTableName(osp_id)
    targetTableName = 'relative_memo_to_open_source_projects'
    if osp_id >= 770000
      targetTableName = 'relative_memo_to_open_source_projects_70'
    else
      a = 1 + osp_id/11000
      targetTableName = 'relative_memo_to_open_source_projects_' + a.to_s
    end
    # if osp_id < 500
      # targetTableName = 'relative_memo_to_open_source_projects_1'
    # elsif osp_id >= 500 && osp_id < 1000
      # targetTableName = 'relative_memo_to_open_source_projects_2'
    # elsif osp_id >= 1000 && osp_id < 1500
      # targetTableName = 'relative_memo_to_open_source_projects_3'
    # elsif osp_id >= 1500 && osp_id < 2000
      # targetTableName = 'relative_memo_to_open_source_projects_4'
    # elsif osp_id >= 2000 && osp_id < 3000
      # targetTableName = 'relative_memo_to_open_source_projects_5'
    # elsif osp_id >= 3000 && osp_id < 5000
      # targetTableName = 'relative_memo_to_open_source_projects_6'
    # elsif osp_id >= 5000 && osp_id < 7500
      # targetTableName = 'relative_memo_to_open_source_projects_7'
    # elsif osp_id >= 7500 && osp_id < 10000
      # targetTableName = 'relative_memo_to_open_source_projects_8'
    # elsif osp_id >= 10000 && osp_id < 310000
      # a = 7 + osp_id/5000
      # targetTableName = 'relative_memo_to_open_source_projects_' + a.to_s
    # elsif osp_id >= 310000
      # targetTableName = 'relative_memo_to_open_source_projects_70'
    # end
    return targetTableName
  end

  #solr search
  #searchable do
  #  integer :osp_id
  #  integer :relative_memo_id
  #  float :match_weight
  #end

end
